<template>
  <div>
    <div class="header-abs" v-show="showABS">
      <router-link to="/">
        <div class="header-abs-back iconfont">&#xe697;</div>
      </router-link>
    </div>
    <div class="header-fixed" :style="opacityStyle" v-show="!showABS">
      长隆欢乐世界
      <router-link to="/">
        <div class="header-fixed-back iconfont">&#xe697;</div>
      </router-link>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DetailHeader',
  data () {
    return {
      showABS: true,
      opacityStyle: 0
    }
  },
  activated () {
    // 此处有危险，因为将事件绑定到了window对象上，所以对全局有影响
    window.addEventListener('scroll',this.handleScoll)
  },
  deactivated() {
    // 当页面隐藏的时候就会解绑window对象上的事件，这样其他页面就不会受到影响了
    window.removeEventListener('scroll',this.handleScoll)
  },
  methods: {
    handleScoll () {
      const top = document.documentElement.scrollTop
      if (top > 60) {
        let opacity = top / 140
        // opacity会随着页面滚动越高数值越大 所以判断下 最大值是 1
        opacity = opacity > 1 ? 1 : opacity
        this.opacityStyle = {
          opacity
        }
        this.showABS = false
      } else {
        this.showABS = true
      }
    }
  }
}
</script>

<style lang="less" scope>
@import "~styles/varlibase.less";
  .header-abs{
    position: absolute;
    left: .2rem;
    top: .2rem;
    width: .8rem;
    height: .8rem;
    border-radius: .4rem;
    text-align: center;
    line-height: .8rem;
    background-color: rgba(0, 0, 0, .8);
    .header-abs-back{
      color: #fff;
      font-size: .4rem;
    }
  }
  .header-fixed{
    overflow: hidden;
    height: @headerHight;
    line-height: @headerHight;
    text-align: center;
    background-color: @bgColor;
    color: #fff;
    font-size: .32rem;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 2;
    .header-fixed-back{
      position: absolute;
      top: 0;
      left: 0;
      width: .64rem;
      text-align: center;
      font-size: .6rem;
      color: #fff
    }
  }
</style>
